package com.example.spider;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

public class ComicPicProcessor implements PageProcessor {
    @Override
    public void process(Page page) {
        //漫画页，继续发现a标签
        List<String> links = page.getHtml().$("a").links().all();
        if (links.size() > 0)
            page.addTargetRequests(links);
        List<String> scripts = page.getHtml().xpath("//script[@language='javascript']").all();
        if(scripts.size() > 0){
            // 抓取内容
            String s = "\\+m201001d\\+\"";
            String img = "http://v2.kukudm.com/"
                    + scripts.get(scripts.size() - 1)
                    .split(s)[1].split("'><span style='display:none'>")[0];
            page.putField("img", img);
        }
    }

    @Override
    public Site getSite() {
        return Site.me().setRetryTimes(3).setSleepTime(1000);
    }
}
